
import 'package:flutter/material.dart';

import '../model/home_model.dart';
import 'fwebview.dart';

class GridNav extends StatefulWidget {
  final GridNavModel gridNavmodel;
  const GridNav({super.key,required this.gridNavmodel});

  @override
  State<GridNav> createState() => _GridNavState();
}

class _GridNavState extends State<GridNav> {


  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
        color: Colors.transparent,
      borderRadius: BorderRadius.circular(6),
      clipBehavior: Clip.antiAlias,
      child: FractionallySizedBox(
        widthFactor: 1,
        child: Container(
          color: Colors.white,
          child: Column(
            children: _gridNavItems(context,widget.gridNavmodel),
          ),
        )
      )
    );
  }

  _gridNavItems(BuildContext context,GridNavModel? gridNavmodel){
   List<Widget> list = [];
   list.add(_gridNavItem(context, gridNavmodel?.hotel));
   if (gridNavmodel?.hotel != null){
     list.add(Container(height: 4,color: Colors.white,));
   }
   list.add(_gridNavItem(context, gridNavmodel?.flight));
   if (gridNavmodel?.flight != null){
     list.add(Container(height: 4,color: Colors.white,));
   }
   list.add(_gridNavItem(context, gridNavmodel?.travel));
   return list;
  }

  Widget _gridNavItem(BuildContext context,GridNavItemModel? gridNavItemmodel){
    Color startColor = Color(int.parse('0xff' + (gridNavItemmodel?.startColor ?? "")));
    Color endColor = Color(int.parse('0xff' + (gridNavItemmodel?.endColor ?? "")));
    return Container(
      height: 88,
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [startColor,endColor]),
      ),
      child: Row(
        // mainAxisAlignment:MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            flex: 1,
            child: Container(
              child: _mainItem(context,gridNavItemmodel?.mainItem),
            ),
          ),

          Container(
            color: Colors.white,
            width: 1,
            // color: Colors.red,
          ),

          Expanded(
            flex: 1,
            child: _doubleItem(context,gridNavItemmodel?.item1,gridNavItemmodel?.item2),
          ),

          Container(
            color: Colors.white,
            width: 1,
            // color: Colors.red,
          ),
          Expanded(
            flex: 1,
            child: _doubleItem(context,gridNavItemmodel?.item3,gridNavItemmodel?.item4),
          ),

        ],
      ),
    );
  }

  _mainItem(BuildContext context,CommonModel? commonModel){
    return GestureDetector(
      onTap: (){
        Navigator.push(context,MaterialPageRoute(builder: (context) => FWebView(url: commonModel?.url,title: commonModel?.title,statusBarColor: commonModel?.statusBarColor,hideAppBar: commonModel?.hideAppBar,)));
      },
      child: Stack(
        children: [
          Image.network(
            commonModel!.icon!,
            width: 121,
            height: 88,
            fit: BoxFit.contain,
            alignment: Alignment.bottomCenter,
          ),
          Positioned(
              left: 4,
              right: 4,
              top: 15,
              child: Text(
                commonModel?.title ?? "",
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.white,
                ),
                textAlign: TextAlign.center,
              )
          ),
        ],
      ),
    );
  }

  _doubleItem(BuildContext context,CommonModel? oneCommonModel,CommonModel? twoCommonModel){
    return Column(
      children: [
        Expanded(
          child: Center(
              child: GestureDetector(
                  onTap: (){
                    Navigator.push(context,MaterialPageRoute(builder: (context) => FWebView(url: oneCommonModel?.url,title: oneCommonModel?.title,statusBarColor: oneCommonModel?.statusBarColor,hideAppBar: oneCommonModel?.hideAppBar,)));
                  },
                  child: Text(
                    oneCommonModel?.title ?? "",
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.white,
                    ),
                    textAlign: TextAlign.center,
                  )
              ),
          ),
        ),
        Container(
          color: Colors.white,
          height: 1,
          // color: Colors.red,
        ),
        Expanded(
          child: Center(
            child: GestureDetector(
                onTap: (){
                  Navigator.push(context,MaterialPageRoute(builder: (context) => FWebView(url: twoCommonModel?.url,title: twoCommonModel?.title,statusBarColor: twoCommonModel?.statusBarColor,hideAppBar: twoCommonModel?.hideAppBar,)));
                },
                child: Text(
                  twoCommonModel?.title ?? "",
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.white,
                  ),
                  textAlign: TextAlign.center,
                )
            ),
          ),
        ),
      ],
    );
  }

}
